
const { ccclass, property } = cc._decorator;

enum EState {
    idle,
    attack,
    moveLeft,
    moveRight,
    jump,
}

@ccclass
export default class NewClass extends cc.Component {

    speed: number = 300;
    sp: cc.Vec2 = cc.v2(0, 0)

    Input: Array<boolean> = []

    heroState = EState.idle;
    heroAnim: string = null;
    combo: number = 0;

    onLoad() {

    }

    start() {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this)
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this)
    }

    setAnimation(clip: cc.AnimationClip) {

    }

    update(dt: number) {
        this.switchState();
    }

    onDestroy() {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this)
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this)
    }

    onKeyDown(e: cc.Event.EventKeyboard) {
        this.Input[e.keyCode] = true
    }

    onKeyUp(e: cc.Event.EventKeyboard) {
        this.Input[e.keyCode] = false
    }

    switchState() {
        let clip: string = null
        //移动
        if (this.Input[cc.macro.KEY.a] || this.Input[cc.macro.KEY.left]) {
            //左
            this.heroMove(EState.moveLeft);
        }
        else if (this.Input[cc.macro.KEY.d] || this.Input[cc.macro.KEY.right]) {
            //右
            this.heroMove(EState.moveRight);
        }
        else {
            //站立
            this.heroMove(EState.idle);
        }
        //攻击
        if (this.Input[cc.macro.KEY.j]) {
            this.heroAttack();
        }
        //跳跃
        if (this.Input[cc.macro.KEY.space] || this.Input[cc.macro.KEY.k]) {
            this.heroJump();
        }
    }
    //攻击状态
    heroAttack() {
        let clip: string = null
        let animation = this.node.getComponent(cc.Animation);
        switch (this.heroState) {
            case EState.idle:
            case EState.moveLeft:
            case EState.moveRight:
                //case EState.attack:
                if (this.Input[cc.macro.KEY.j]) {
                    if (this.combo == 0) {
                        clip = 'hero_attack1'
                        this.combo = 1;
                    }
                    else if (this.combo == 1) {
                        clip = 'hero_attack2'
                        this.combo = 2;
                    }
                    else if (this.combo == 2) {
                        clip = 'hero_attack3'
                        this.combo = 0;
                    }
                }
                break
        }
        if (clip != null && this.heroAnim != clip) {
            this.heroAnim = clip;
            this.heroState = EState.attack;
            animation.play(clip);
        }
    }
    //结束攻击
    finshAtt() {
        this.heroState = EState.idle;
        setTimeout(() => {
            if (this.heroState == EState.attack) return;
            this.combo = 0;
        }, 1000)
    }

    //移动
    heroMove(state: EState) {
        let rigid = this.node.getComponent(cc.RigidBody);
        let lv = rigid.linearVelocity
        switch (this.heroState) {
            case EState.attack:
                lv.x = 0;
                rigid.linearVelocity = lv;
                return;
        }
        let animation = this.node.getComponent(cc.Animation);
        let clip: string = null

        let scaleX = Math.abs(this.node.scaleX);
        if (state == EState.moveLeft) {
            this.sp.x = -1;
            this.node.scaleX = -scaleX
            clip = 'hero_run'
        }
        else if (state == EState.moveRight) {
            this.sp.x = 1
            this.node.scaleX = scaleX
            clip = 'hero_run'
        }
        else if (state == EState.idle) {
            this.sp.x = 0;
            clip = 'hero_idle'
        }


        //移动
        if (this.sp.x) {
            lv.x = this.sp.x * this.speed
        }
        else {
            lv.x = 0;
        }
        rigid.linearVelocity = lv;
        if (clip != null && this.heroAnim != clip) {
            this.heroAnim = clip;
            this.heroState = state;
            animation.play(clip);
        }
    }

    //跳跃
    heroJump() {
        switch (this.heroState) {
            case EState.jump:
                return
        }
        let rigid = this.node.getComponent(cc.RigidBody);
        let lv = rigid.linearVelocity;
        lv.y = 300;
        rigid.linearVelocity = lv;
    }

   

}
